package data.dictionary.saver;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;

import data.dictionary.Dictionary_Memory;

import tools.Pair;

public class DictionarySaver<T> {

	private String filename;
	private Dictionary_Memory<T> dico;

	public DictionarySaver(String filename, Dictionary_Memory<T> dico) {
		super();
		this.filename = filename;
		this.dico = dico;
	}

	private void save(PrintStream p) {
		
		Iterator<T> iter = dico.iterator();
		
		while(iter.hasNext()) {
			p.println(iter.next()); // 1 par ligne
		}
	}

	public void save() {
		try {
			FileOutputStream output = new FileOutputStream(filename);
			PrintStream p = new PrintStream(output);
			save(p);

			output.close();
		} catch (IOException e) {
			System.err.println("Can't open file " + filename
					+ " for writting... Saving aborted");

		}
	}

}
